using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Zhao.Arena.MiniProgram.Api.Utility;
using Zhao.Arena.MiniProgram.Api.Utility.AuthorizationExt;
using Zhao.Arena.MiniProgram.Api.Utility.InitDatabaseExt;
using Zhao.Arena.MiniProgram.Api.Utility.RegisterExt;
using Zhao.Arena.MiniProgram.Api.Utility.SwaggerExt;
using Zhao.Common.AliyunUtil.Model;
using Zhao.Common.Applet;
using Zhao.Common.Applet.Model;
using Zhao.Common.Helper;
using Zhao.Common.JWT;
using Zhao.Common.MapConfig;
using Zhao.Service;

namespace Zhao.Arena.MiniProgram.Api
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            //配置跨域
            builder.CorsDomainsPolicy();
            //直接调用扩展方法 配置swagger
            builder.AddSwaggerExt();
            //初始化SqlSugar--注册到IOC容器
            builder.InitSqlSugar();
            //配置阿里云存储OSS Api配置
            builder.Services.Configure<AliyunOSSSetting>(builder.Configuration.GetSection("AliyunOSSSetting"));
            //配置节假日json文件的地址
            builder.Services.Configure<HolidaySettings>(builder.Configuration.GetSection("HolidaySettings"));

            // 添加内存缓存
            builder.Services.AddMemoryCache();

            #region JWT配置
            // 配置JWT
            builder.Services.Configure<JWTTokenOptions>(builder.Configuration.GetSection("JwtSettings"));

            //鉴权
            builder.AuthorizationExt();

            #endregion

            #region 微信小程序注册
            // 配置微信的选项类
            builder.Services.Configure<WeChatOptions>(
                builder.Configuration.GetSection("WeChatKey"));

            // 注册微信客户端
            builder.Services.AddScoped<WechatAPIClient>(provider =>
            {
                var options = provider.GetRequiredService<IOptions<WeChatOptions>>().Value;

                // 参数验证
                if (string.IsNullOrEmpty(options.AppID) || string.IsNullOrEmpty(options.AppSecret))
                {
                    throw new ApplicationException("微信配置信息不完整");
                }
                return new WechatAPIClient(options.AppID, options.AppSecret);
            });
            #endregion

            #region automapper
            builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
            #endregion

            //注册注入集合
            builder.RegistService();
            builder.RegistControllers();

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();



            #region 项目加载的时候预加载地区数据
            // 从容器中获取 AreaCacheManager 并预加载
            using (var scope = app.Services.CreateScope())
            {
                var cacheManager = scope.ServiceProvider.GetRequiredService<AreaCacheManager>();
                await cacheManager.InitializeCache(); // 预加载省市区数据到缓存
            }
            #endregion

            //配置跨域策略生效
            app.UseCorsDomainsPolicy();

            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //{
            //    app.UseSwagger();
            //    app.UseSwaggerUI();
            //}

            app.UserSgwaagerExt();

            app.UseHttpsRedirection();

            #region jwt 注册服务
            app.UseAuthentication();//鉴权
            app.UseAuthorization();//授权
            #endregion

            app.MapControllers();

            app.Run();
        }
    }
}
